package com.easylinkin.linkappapi.meterbilling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.meterbilling.entity.RoomInfo;
import com.easylinkin.linkappapi.meterbilling.mapper.RoomInfoMapper;
import com.easylinkin.linkappapi.meterbilling.service.CbjfLinkappAreaService;
import com.easylinkin.linkappapi.security.constant.LinkappUserConstant;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import com.easylinkin.linkappapi.space.entity.LinkappArea;
import com.easylinkin.linkappapi.space.entity.LinkappSpace;
import com.easylinkin.linkappapi.space.mapper.LinkappAreaMapper;
import com.easylinkin.linkappapi.space.mapper.LinkappSpaceMapper;
import com.easylinkin.linkappapi.space.vo.SpaceTreeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @program: linkapp-group
 * @description: 对区域做处理
 * @author: chenkaixuan
 * @create: 2021-07-12 14:24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CbjfLinkappLinkappAreaServiceImpl extends ServiceImpl<LinkappAreaMapper, LinkappArea> implements CbjfLinkappAreaService {
    @Resource
    LinkappUserContextProducer linkappUserContextProducer;
    @Resource
    LinkappAreaMapper linkappAreaMapper;
    @Resource
    LinkappSpaceMapper linkappSpaceMapper;
    @Resource
    RoomInfoMapper roomInfoMapper;

    @Override
    public List<SpaceTreeVo> selectLinkappAreaTreeList(LinkappArea linkappArea) {
        LinkappUser user = linkappUserContextProducer.getCurrent();
        List<SpaceTreeVo> spaceTreeVos;
        if (LinkappUserConstant.ADMIN_TYPE.equals(user.getType())) {
            spaceTreeVos = linkappAreaMapper.selectLinkappAreaTreeList(linkappArea);
        } else {
            List<LinkappSpace> spaceList = linkappSpaceMapper.selectLinkappSpaceByUser(user.getId().toString());
            List<String> spaces = new ArrayList<String>();
            if (spaceList.isEmpty()) {
                spaces.add("-1");
                linkappArea.setSpaceIds(spaces);
            } else {
                for (LinkappSpace space : spaceList) {
                    spaces.add(space.getId());
                }
                linkappArea.setSpaceIds(spaces);
            }
            spaceTreeVos=linkappAreaMapper.selectLinkappAreaTreeList(linkappArea);
        }
        //对区域空间绑定房间，并对数据组装
        if(spaceTreeVos != null && spaceTreeVos.size()>0){
//            //获取所有的父节点
//            Set<String> parentNodeIds = spaceTreeVos.stream().filter(i -> !StringUtils.isEmpty(i.getParentId())).map(i -> i.getParentId()).collect(Collectors.toSet());
//            //获取所有叶子节点
//            Set<String> childNodeIds = spaceTreeVos.stream().filter(i -> !parentNodeIds.contains(i.getId())).map(i -> i.getId()).collect(Collectors.toSet());
            //获取所有叶子节点的上级
            Map<String, SpaceTreeVo> spaceTreeVoMap = spaceTreeVos.stream().collect(Collectors.toMap(i -> i.getId(), i -> i, (k1, k2) -> k1));
            //用子节点查询房间
            QueryWrapper<RoomInfo> roomInfoQueryWrapper = new QueryWrapper<>();
            roomInfoQueryWrapper.eq("tenant_id",user.getTenantId());
//            roomInfoQueryWrapper.in("area_id",childNodeIds);
            roomInfoQueryWrapper.ne("delete_status",1);
            List<RoomInfo> roomInfos = roomInfoMapper.selectList(roomInfoQueryWrapper);
            //房间数据封装
            String area="area";
            String room="room";
            if(roomInfos != null && roomInfos.size()>0){
                roomInfos.stream().forEach(i->{
                    SpaceTreeVo parentSpaceTreeVo = spaceTreeVoMap.get(i.getAreaId());
                    if (StringUtils.isEmpty(parentSpaceTreeVo)) {
                        return;
                    }
                    SpaceTreeVo childSpaceTreeVo = new SpaceTreeVo();
                    childSpaceTreeVo.setId(i.getId() + "");
                    childSpaceTreeVo.setName(i.getRoomName());
                    childSpaceTreeVo.setAreaPath(parentSpaceTreeVo.getAreaPath() + ":" + i.getRoomName());
                    childSpaceTreeVo.setParentId(i.getAreaId());
                    if(area.equals(parentSpaceTreeVo.getNodeType())){
                        childSpaceTreeVo.setSpaceId(parentSpaceTreeVo.getSpaceId());
                    }else{
                        childSpaceTreeVo.setSpaceId(parentSpaceTreeVo.getId());
                    }
                    childSpaceTreeVo.setRoomIdentification(room);
                    childSpaceTreeVo.setLevel(parentSpaceTreeVo.getLevel() + 1);
                    childSpaceTreeVo.setNodeType(area);
                    childSpaceTreeVo.setTenantId(user.getTenantId());
                    spaceTreeVos.add(childSpaceTreeVo);
                });
            }
        }
        return spaceTreeVos;
    }
}
